﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCentral.Forms;
using System.Data;
using DataCentral.Dataset;
using DataCentral.BusinessDataObjects;
using CommonData.BusinessDataObjects;
using CommonData;

namespace DataCentral.UsecaseControllers
{
    public class SynchronizeUsecase
    {
        public void StartUsecase(DataCentralDataset dsCentral,bool isEdit, DataCentralDataset.TablesConfigurationRow drTablesConfig)
        {
            TablesConfigurationForm frmTBLConfig = new TablesConfigurationForm(this,dsCentral,isEdit,drTablesConfig);
            frmTBLConfig.ShowDialog();
        }

        public DataTable GetTableList(string database)
        {
            CommonDataAccess cmData = new CommonDataAccess();
            DataSet dsDatabase = cmData.GetDataSetBySproc("USYS_GetUserTablesList", null);
            foreach (DataTable tbl in dsDatabase.Tables)
            {
                for (int i = 0; i < tbl.Rows.Count; i++)
                {
                    if (tbl.Rows[i]["TABLE_CATALOG"].ToString().ToUpper().Equals(database.ToUpper()))
                    {
                        return tbl;
                    }
                }
            }
            return null;
        }

        public List<string>GetDatatypeList()
        {
            List<string> lstDatatype = new List<string>();
            foreach (string datatype in Enum.GetNames(typeof(System.TypeCode)))
            {
                lstDatatype.Add(datatype);
            }
            return lstDatatype;
        }

        public DataTable GetColumnList(string strConnection,string tableName)
        {
            CommonDataAccess cmDataaccess = new CommonDataAccess();
            string SQL = "SELECT COLUMN_NAME, DATA_TYPE FROM information_schema.columns WHERE TABLE_NAME = '" + tableName +"'";
            return cmDataaccess.GetDataTableBySQL(strConnection, SQL,null);
        }

        public void SynchronizeAllDataToDestTable(decimal configID, string sourceConn, int sourceDB, string sourceTable,string destTable, bool isClearBefore,bool isAllData)
        {
            SynchronizeBO boSynchronize = new SynchronizeBO();
            boSynchronize.SynchronizeSourceData(configID, sourceConn, sourceDB, sourceTable, destTable, isClearBefore, isAllData);
        }
    }
}
